package com.zhuangfei.hpuhelper.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zhuangfei.generalpage.BaseActivity;
import com.zhuangfei.toolkit.IntentToolkit;
import com.zhuangfei.toolkit.model.BundleModel;
import com.zhuangfei.toolkit.tools.ActivityTools;
import com.zhuangfei.toolkit.tools.BundleTools;
import com.zhuangfei.toolkit.tools.ShareTools;

import cn.lzf.serviceplatform.R;

public class WebViewActivity extends BaseActivity {

    // wenview与加载条
    WebView webView;

    // 关闭
    private LinearLayout closeLayout;
    Class returnClass;

    // 标题
    TextView titleTextView;
    String title, url;
    int fromItem=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        ActivityTools.setTransparent(this);
        initUrl();
        initView();
        loadWebView();
    }

    private void initUrl() {
        returnClass = BundleTools.getFromClass(this, MainActivity.class);
        fromItem=BundleTools.getFromItem(this,0);
        title = (String) BundleTools.getObject(this, "title", "WebView");
        url = BundleTools.getString(this, "url", "http://www.liuzhuangfei.com");
        fromItem=BundleTools.getFromItem(this,0);
    }

    private void initView() {
        webView = (WebView) findViewById(R.id.id_webview);
        titleTextView = (TextView) findViewById(R.id.id_web_title);
        titleTextView.setText(title);

        closeLayout = (LinearLayout) findViewById(R.id.id_close);

        closeLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                new IntentToolkit().with(WebViewActivity.this)
                        .target(returnClass)
                        .canBack()
                        .toItem(fromItem)
                        .execute();
            }
        });
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void loadWebView() {

        webView.loadUrl(url);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDefaultTextEncodingName("gb2312");
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);

        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setDisplayZoomControls(false);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                webView.loadUrl(url);
                return true;
            }

        });

        webView.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent,
                                        String contentDisposition, String mimetype,
                                        long contentLength) {
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress >=90) {
                    getViewHelper().showSuccessView();
                }
            }
        });

    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            new IntentToolkit().with(WebViewActivity.this)
                    .target(returnClass)
                    .canBack()
                    .toItem(fromItem)
                    .execute();
        }

    }
}
